﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Carer.Data;
using Carer.Data.Models;
using System.Runtime.InteropServices;
using System.Web.Mvc;
using Carer.Crm.Authorize;

namespace Carer.Crm.Controllers
{
    [SessionAuthorize]
    public class MaintainController : BaseController
    {
        public MaintainController()
        {

        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult CategoryPage(int page)
        {
            CategoryQuery q = base.Session["CategoryQuery"] as CategoryQuery;
            if (q == null)
            {
                CategoryQuery query2 = new CategoryQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["CategoryQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetCategories(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult CategoryView()
        {
            return base.ViewModel(new List<string>() { "信息维护", "服务类别" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult CrewTypePage(int page)
        {
            CrewTypeQuery q = base.Session["CrewTypeQuery"] as CrewTypeQuery;
            if (q == null)
            {
                CrewTypeQuery query2 = new CrewTypeQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["CrewTypeQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetCrewTypes(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult CrewTypeView()
        {
            return base.ViewModel(new List<string>() { "信息维护", "人员性质" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCategories(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条服务类别记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteCategory(ids))
            {
                return base.Json(new SubmitResult(true, "服务类别记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "服务类别记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCategory(int id)
        {
            if (DataService.DeleteCategory(id))
            {
                return base.Json(new SubmitResult(true, "服务类别记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "服务类别记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCrewType(int id)
        {
            if (DataService.DeleteCrewType(id))
            {
                return base.Json(new SubmitResult(true, "人员性质记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "人员性质记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteCrewTypes(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条人员性质记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteCrewType(ids))
            {
                return base.Json(new SubmitResult(true, "人员性质记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "人员性质记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeletePayment(int id)
        {
            if (DataService.DeletePayment(id))
            {
                return base.Json(new SubmitResult(true, "结算记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "结算记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeletePayments(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条结算记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeletePayment(ids))
            {
                return base.Json(new SubmitResult(true, "结算记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "结算记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteService(int id)
        {
            if (DataService.DeleteService(id))
            {
                return base.Json(new SubmitResult(true, "服务项目记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "服务项目记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteServices(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条服务项目记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteService(ids))
            {
                return base.Json(new SubmitResult(true, "服务项目记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "服务项目记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditCategory(Category u)
        {
            if (string.IsNullOrEmpty(u.Name))
            {
                return base.Json(new SubmitResult(false, "服务类别名称不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            u.UnitId = this.GetUserUnitId();
            if (!u.UnitId.HasValue)
            {
                return base.Json(new SubmitResult(false, "未选择一家单位"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditCategory(u))
            {
                return base.Json(new SubmitResult(true, "服务类别数据保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "服务类别数据保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditCrewType(CrewType u)
        {
            if (string.IsNullOrEmpty(u.Name))
            {
                return base.Json(new SubmitResult(false, "人员性质不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            u.UnitId = this.GetUserUnitId();
            if (!u.UnitId.HasValue)
            {
                return base.Json(new SubmitResult(false, "未选择一家单位"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditCrewType(u))
            {
                return base.Json(new SubmitResult(true, "人员性质记录保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "人员性质记录保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditPayment(Payment u)
        {
            if (string.IsNullOrEmpty(u.Name))
            {
                return base.Json(new SubmitResult(false, "名称不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!u.Ratio.HasValue)
            {
                return base.Json(new SubmitResult(false, "结算比例不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            u.UnitId = this.GetUserUnitId();
            if (!u.UnitId.HasValue)
            {
                return base.Json(new SubmitResult(false, "未选择一家单位"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditPayment(u))
            {
                return base.Json(new SubmitResult(true, "结算记录保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "结算记录保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditService(Service u)
        {
            if (string.IsNullOrEmpty(u.Name) || string.IsNullOrEmpty(u.Type))
            {
                return base.Json(new SubmitResult(false, "服务项目和服务类型不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (string.IsNullOrEmpty(u.Unit) || (u.Price == 0.0))
            {
                return base.Json(new SubmitResult(false, "单位和价格不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            u.UnitId = this.GetUserUnitId();
            if (!u.UnitId.HasValue)
            {
                return base.Json(new SubmitResult(false, "未选择一家单位"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditService(u))
            {
                return base.Json(new SubmitResult(true, "服务项目数据保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "服务项目数据保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetCategory(int id)
        {
            return base.Json(DataService.GetCategory(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetCrewType(int id)
        {
            return base.Json(DataService.GetCrewType(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetPayment(int id)
        {
            return base.Json(DataService.GetPayment(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetService(int id)
        {
            return base.Json(DataService.GetService(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult PaymentPage(int page)
        {
            PaymentQuery q = base.Session["PaymentQuery"] as PaymentQuery;
            if (q == null)
            {
                PaymentQuery query2 = new PaymentQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["PaymentQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetPayments(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult PaymentView()
        {
            int? hid = this.GetUserUnitId();
            ViewBag.Services = DataService.GetAllServices(hid);
            ViewBag.CrewTypes = DataService.GetAllCrewTypes(hid);
            return base.ViewModel(new List<string>() { "信息维护", "结算标准" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryCategory(CategoryQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["CategoryQuery"] as CategoryQuery;
                if (q == null)
                {
                    CategoryQuery query = new CategoryQuery
                    {
                        UnitId = this.GetUserUnitId()
                    };
                    base.Session["CategoryQuery"] = q = query;
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["CategoryQuery"] = q;
            }
            return base.Json(DataService.GetCategories(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryCrewType(CrewTypeQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["CrewTypeQuery"] as CrewTypeQuery;
                if (q == null)
                {
                    CrewTypeQuery query = new CrewTypeQuery
                    {
                        UnitId = this.GetUserUnitId()
                    };
                    base.Session["CrewTypeQuery"] = q = query;
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["CrewTypeQuery"] = q;
            }
            return base.Json(DataService.GetCrewTypes(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryPayment(PaymentQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["PaymentQuery"] as PaymentQuery;
                if (q == null)
                {
                    PaymentQuery query = new PaymentQuery
                    {
                        UnitId = this.GetUserUnitId()
                    };
                    base.Session["PaymentQuery"] = q = query;
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["PaymentQuery"] = q;
            }
            return base.Json(DataService.GetPayments(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryService(ServiceQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["ServiceQuery"] as ServiceQuery;
                if (q == null)
                {
                    ServiceQuery query = new ServiceQuery
                    {
                        UnitId = this.GetUserUnitId()
                    };
                    base.Session["ServiceQuery"] = q = query;
                }
            }
            else
            {
                q.UnitId = this.GetUserUnitId();
                base.Session["ServiceQuery"] = q;
            }
            return base.Json(DataService.GetServices(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult ServicePage(int page)
        {
            ServiceQuery q = base.Session["ServiceQuery"] as ServiceQuery;
            if (q == null)
            {
                ServiceQuery query2 = new ServiceQuery
                {
                    UnitId = this.GetUserUnitId()
                };
                base.Session["ServiceQuery"] = q = query2;
            }
            q.PageNumber = page;
            return base.Json(DataService.GetServices(q), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult ServiceView()
        {
            var items = new List<SelectListItem>() { new SelectListItem() { Text = "所有", Value = "", Selected = true } };
            var userCategories = DataService.GetAllCategories(this.GetUserUnitId());
            var userItems = userCategories.Select(it => new SelectListItem() { Text = it.Name, Value = it.Name, Selected = false }).ToList();
            items.AddRange(userItems);

            ViewBag.Categories = items;
            return base.ViewModel(new List<string>() { "信息维护", "服务项目" });
        }

    }
}
